package com.bsdsma.fengheguaiapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.bsdsma.fengheguaiapp.dto.AdmininfoAddDto;
import com.bsdsma.fengheguaiapp.dto.AdmininfoSearchDto;
import com.bsdsma.fengheguaiapp.dto.DataGroupByDateDto;
import com.bsdsma.fengheguaiapp.entity.Admininfo;
import com.bsdsma.fengheguaiapp.enums.Role;
import com.bsdsma.fengheguaiapp.mapper.AdmininfoMapper;
import com.bsdsma.fengheguaiapp.service.AdmininfoService;
import com.bsdsma.fengheguaiapp.constants.ErrorCodes;
import com.bsdsma.fengheguaiapp.dto.PageRetDto;
import com.bsdsma.fengheguaiapp.exceptionhandler.GlobalRuntimeException;
import com.bsdsma.fengheguaiapp.utils.CustomerBeanUtils;
import com.bsdsma.fengheguaiapp.utils.DateUtils;
import com.bsdsma.fengheguaiapp.utils.PageRetUtils;
import com.bsdsma.fengheguaiapp.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AdmininfoServiceImpl implements AdmininfoService {

    /**
     * 使用Spring注入Mybatis操作对象
     */
    @Autowired
    private AdmininfoMapper mapper;

    @Override
    public PageRetDto<Admininfo> search(AdmininfoSearchDto dto) {
        Admininfo bean = new Admininfo();
        bean.setStatus(0);
        QueryWrapper<Admininfo> wrapper = new QueryWrapper<>(bean);

        if (!StringUtils.isBlank(dto.getAdminName())) {
            wrapper.like("adminName", "%" + dto.getAdminName() + "%");
        }
        if (!StringUtils.isBlank(dto.getAdminPass())) {
            wrapper.like("adminPass", "%" + dto.getAdminPass() + "%");
        }
        IPage<Admininfo> pageData = mapper.selectPage(new PageDTO<>(dto.getPage(), dto.getSize()), wrapper);
        return PageRetUtils.parse(pageData);
    }

    @Override
    public List<Admininfo> listAll() {
        Admininfo bean = new Admininfo();
        bean.setStatus(0);
        return mapper.selectList(new QueryWrapper<>(bean));
    }

    @Override
    public Admininfo add(AdmininfoAddDto dto) {
        Admininfo bean = CustomerBeanUtils.copyPropertiesClass(dto, Admininfo.class);
        String adminName = dto.getAdminName();
        String adminPass = dto.getAdminPass();
        String adminPhone = dto.getAdminPhone();
        String adminEmail = dto.getAdminEmail();

        if (StringUtils.isBlank(adminName)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写管理员账号");
        }
        if (StringUtils.isBlank(adminPass)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写管理员密码");
        }
        if (StringUtils.isBlank(adminPhone)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写管理员电话");
        }
        if (StringUtils.isBlank(adminEmail)) {
            bean.setAdminEmail("");
        }
        bean.setStatus(0);
        bean.setCt(DateUtils.getNow());
        bean.setUt(DateUtils.getNow());
        mapper.insert(bean);
        return bean;
    }

    @Override
    public Admininfo update(Long id, AdmininfoAddDto dto) {
        Admininfo bean = this.getById(id);
        if (null == bean) {
            throw new GlobalRuntimeException(ErrorCodes.OBJ_NOT_EXISTS, "管理员信息不存在");
        }
        String adminName = dto.getAdminName();
        String adminPass = dto.getAdminPass();
        String adminPhone = dto.getAdminPhone();
        String adminEmail = dto.getAdminEmail();

        if (StringUtils.isBlank(adminName)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写管理员账号");
        }
        bean.setAdminName(adminName);
        if (StringUtils.isBlank(adminPass)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写管理员密码");
        }
        bean.setAdminPass(adminPass);
        if (StringUtils.isBlank(adminPhone)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写管理员电话");
        }
        bean.setAdminPhone(adminPhone);
        if (StringUtils.isBlank(adminEmail)) {
            bean.setAdminEmail("");
        }else{
            bean.setAdminEmail(adminEmail);
        }
        mapper.updateById(bean);
        return bean;
    }

    @Override
    public Admininfo getById(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public void delete(Long id) {
        Admininfo bean = this.getById(id);
        if (null == bean) {
            throw new GlobalRuntimeException(ErrorCodes.OBJ_NOT_EXISTS, "管理员信息不存在");
        }
        bean.setStatus(-1);
        bean.setUt(DateUtils.getNow());
        mapper.updateById(bean);
    }

    @Override
    public int updateById(Admininfo bean) {
        return mapper.updateById(bean);
    }

    @Override
    public Long count() {
        Admininfo bean = new Admininfo();
        bean.setStatus(0);
        return mapper.selectCount(new QueryWrapper<>(bean));
    }

    @Override
    public List<DataGroupByDateDto> countByDay(Date beginDate, Date endDate) {
        return mapper.countByDay(beginDate, endDate);
    }
}